package com.lipp.luosu.controller;

import com.lipp.luosu.common.Result;
import com.lipp.luosu.dto.req.BillSearchReqDto;
import com.lipp.luosu.dto.resp.PageRespDto;
import com.lipp.luosu.pojo.Bill;
import com.lipp.luosu.service.BillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.UUID;

@RestController
@RequestMapping("/bill")
@Slf4j
@CrossOrigin(origins = "*") // 允许所有源访问
public class BillController {

    @Resource
    BillService billService;
    @PostMapping("/getBillList")
    public Result getBillList(@RequestBody BillSearchReqDto billSearchReqDto) {
        log.info("=======================分页查询======================");
        log.info("======================传递的参数======================");
        log.info(billSearchReqDto.toString()+billSearchReqDto.getPageNum()+""+ billSearchReqDto.getPageSize());
        Result<PageRespDto<Bill>> result = new Result<>(); // 创建一个Result对象，包含一个PageRespDto<Bill>对象
        PageRespDto<Bill> list = billService.getList(billSearchReqDto); // 调用billService的getList方法获取满足条件的Bill对象列表
        result.setData(list); // 将列表赋值给Result对象的data属性
        log.info("======================返回的参数======================");
        log.info(result.toString());
        log.info("=======================分页查询结束======================");
        return result; // 返回Result对象

    }

    @PostMapping("/deleteBill")
    public Result deleteBill(@RequestBody  BillSearchReqDto dto) {
        log.info("=======================删除======================");
        log.info("======================传递的参数id======================");
        log.info(dto.toString());
        Result<String> result = new Result<>(); // 创建一个Result对象，包含一个String对象
        billService.deleteBill(dto.getId()); // 调用billService的deleteBill方法删除Bill对象
        result.setData("删除成功"); // 将字符串赋值给Result对象的data属性
        log.info("======================删除结束======================");
        return result;
    }

    /**
     * 保存账单
     * @param bill
     * @return
     */
    @PostMapping("/saveBill")
    public Result updateBill(@RequestBody  Bill bill) {
        log.info("======================保存传递的参数======================");
        Result<String> result = new Result<>(); // 创建一个Result对象，包含一个String对象
        try{
            log.info(bill.toString());
            billService.saveBill(bill); // 调用billService的updateBill方法更新Bill对象
            result.setData("更新成功"); // 将字符串赋值给Result对象的data属性
        }catch (Exception e){
            result.setData("更新失败原因是："+e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

}
